﻿namespace ManagerClinic.DonThuoc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmDonThuocChiTiet : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler donThuocChiTietEvent = null;
        DonThuocChiTietEntity donThuocChiTietEntity = null;

        DmVatTuBusiness dmVatTuBusiness = new DmVatTuBusiness();
        DmPhanLoaiVatTuBusiness dmPhanLoaiVatTuBusiness = new DmPhanLoaiVatTuBusiness();
        DmCachDungBusiness dmCachDungBusiness = new DmCachDungBusiness();
        DmDonViBusiness dmDonViBusiness = new DmDonViBusiness();
        DmGhiChuCachDungBussiness dmGhiChuCachDungBussiness = new DmGhiChuCachDungBussiness();
        DmLoaiVatTuBusiness dmLoaiVatTuBusiness = new DmLoaiVatTuBusiness();
        DonThuocChiTietBusiness donThuocChiTietBusiness = new DonThuocChiTietBusiness();

        string maDongThuoc = string.Empty;
        int state = 0;

        public frmDonThuocChiTiet(int _state, string _maDonThuoc)
        {
            InitializeComponent();
            maDongThuoc = _maDonThuoc;
            state = _state;

            LoadLoaiVatTu();
            LoadPhanLoaiVt(lkLoaiVatTu.EditValue.ToString());
            LoadDmVatTu(lkPhanLoaiVatTu.EditValue.ToString());

            LoadDanhMuc();
            lkLoaiVatTu.Focus();
        }

        public frmDonThuocChiTiet(int _state, DonThuocChiTietEntity obj)
        {
            InitializeComponent();

            donThuocChiTietEntity = donThuocChiTietBusiness.GetDonThuocChiTiet(obj.MaVatTu, obj.MaDonThuoc);
            maDongThuoc = donThuocChiTietEntity.MaDonThuoc;
            state = _state;
            LoadDanhMuc();

            lkLoaiVatTu.Properties.ReadOnly = true;
            lkPhanLoaiVatTu.Properties.ReadOnly = true;
            lkDmVatTu.Properties.ReadOnly = true;

            LoadLoaiVatTu();

            lkLoaiVatTu.EditValue = donThuocChiTietEntity.MaLoaiVatTu;
            lkPhanLoaiVatTu.EditValue = donThuocChiTietEntity.MaPhanLoaiVatTu;
            lkDmVatTu.EditValue = donThuocChiTietEntity.MaVatTu;

            lkDonVi.EditValue = donThuocChiTietEntity.MaDonVi;
            lkCachDungThuoc.EditValue = donThuocChiTietEntity.MaCachDung;
            txtSoLanDungThuoc.Text = donThuocChiTietEntity.LanDung.ToString();
            lkDonViDungThuoc.EditValue = donThuocChiTietEntity.MaDonViDung;
            txtSoLuongDungThuoc.Text = donThuocChiTietEntity.SoLuongDung.ToString();
            lkGhiChuCachDung.Text = donThuocChiTietEntity.MaGhiChuCachDung;
            txtSoLuong.Text = donThuocChiTietEntity.SoLuong.ToString();
        }

        #region "Load Danh Muc"
        void LoadLoaiVatTu()
        {
            var lstLoaiVatTuEntity = dmLoaiVatTuBusiness.GetDmLoaiVatTuList();
            lstLoaiVatTuEntity.Insert(0, new DmLoaiVatTuEntity() { MaLoaiVatTu = ConstantManagerClinic.sValueMember, TenLoaiVatTu = ConstantManagerClinic.sDisplayMember });
            lkLoaiVatTu.Properties.DataSource = lstLoaiVatTuEntity;
            lkLoaiVatTu.Properties.ValueMember = "MaLoaiVatTu";
            lkLoaiVatTu.Properties.DisplayMember = "TenLoaiVatTu";
            lkLoaiVatTu.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadPhanLoaiVt(string maLoaiVatTu)
        {
            var lstPhanLoaiVatTuEntity = dmPhanLoaiVatTuBusiness.GetDmPhanLoaiVatTuList(maLoaiVatTu);
            lstPhanLoaiVatTuEntity.Insert(0, new DmPhanLoaiVatTuEntity() { MaPhanLoaiVatTu = ConstantManagerClinic.sValueMember, TenPhanLoaiVatTu = ConstantManagerClinic.sDisplayMember });

            lkPhanLoaiVatTu.Properties.DataSource = lstPhanLoaiVatTuEntity;
            lkPhanLoaiVatTu.Properties.ValueMember = "MaPhanLoaiVatTu";
            lkPhanLoaiVatTu.Properties.DisplayMember = "TenPhanLoaiVatTu";
            lkPhanLoaiVatTu.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmVatTu(string maPhanLoaiVatTu)
        {
            var lstDmVatTuEntity = dmVatTuBusiness.GetDmVatTuList(maPhanLoaiVatTu);
            lstDmVatTuEntity.Insert(0, new DmVatTuEntity() { MaVatTu = ConstantManagerClinic.sValueMember, TenVatTu = ConstantManagerClinic.sDisplayMember });
            
            lkDmVatTu.Properties.DataSource = lstDmVatTuEntity;
            lkDmVatTu.Properties.ValueMember = "MaVatTu";
            lkDmVatTu.Properties.DisplayMember = "TenVatTu";
            lkDmVatTu.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmCachDung()
        {
            var lstDmCachDungEntity = dmCachDungBusiness.GetDmCachDungList();
            lstDmCachDungEntity.Insert(0, new DmCachDungEntity() { MaCachDung = ConstantManagerClinic.sValueMember, TenCachDung = ConstantManagerClinic.sDisplayMember });
            lkCachDungThuoc.Properties.DataSource = lstDmCachDungEntity;
            lkCachDungThuoc.Properties.DisplayMember = "TenCachDung";
            lkCachDungThuoc.Properties.ValueMember = "MaCachDung";
            lkCachDungThuoc.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmDonVi()
        {
            var lstDmDonViEntiy = dmDonViBusiness.GetDmDonViList();
            lstDmDonViEntiy.Insert(0, new DmDonViEntity() { MaDonVi = ConstantManagerClinic.sValueMember, TenDonVi = ConstantManagerClinic.sDisplayMember });
            lkDonVi.Properties.DataSource = lstDmDonViEntiy;
            lkDonVi.Properties.DisplayMember = "TenDonVi";
            lkDonVi.Properties.ValueMember = "MaDonVi";
            lkDonVi.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmDonViCachDung()
        {
            var lstDmDonViEntiy = dmDonViBusiness.GetDmDonViList();
            lstDmDonViEntiy.Insert(0, new DmDonViEntity() { MaDonVi = ConstantManagerClinic.sValueMember, TenDonVi = ConstantManagerClinic.sDisplayMember });
            lkDonViDungThuoc.Properties.DataSource = lstDmDonViEntiy;
            lkDonViDungThuoc.Properties.DisplayMember = "TenDonVi";
            lkDonViDungThuoc.Properties.ValueMember = "MaDonVi";
            lkDonViDungThuoc.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmGhiChuCachDung()
        {
            var lstGhiChuCachDungEntity = dmGhiChuCachDungBussiness.GetDmGhiChuCachDungList();
            lstGhiChuCachDungEntity.Insert(0, new DmGhiChuCachDungEntity() { MaGhiChuCachDung = ConstantManagerClinic.sValueMember, TenGhiChuCachDung = ConstantManagerClinic.sDisplayMember });
            lkGhiChuCachDung.Properties.DataSource = lstGhiChuCachDungEntity;
            lkGhiChuCachDung.Properties.ValueMember = "MaGhiChuCachDung";
            lkGhiChuCachDung.Properties.DisplayMember = "TenGhiChuCachDung";
            lkGhiChuCachDung.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDanhMuc()
        {
            LoadDmDonVi();
            LoadDmCachDung();
            LoadDmDonViCachDung();
            LoadDmGhiChuCachDung();
        }
        #endregion

        private void frmDonThuocChiTiet_Load(object sender, EventArgs e)
        {

        }

        private void lkDmVatTu_EditValueChanged(object sender, EventArgs e)
        {
            DmVatTuEntity dmVatTuEntity = (DmVatTuEntity)lkDmVatTu.GetSelectedDataRow();
            if (dmVatTuEntity != null)
            {
                lkDonVi.EditValue = dmVatTuEntity.MaDonVi;
                lkCachDungThuoc.EditValue = dmVatTuEntity.MaCachDung;
                txtSoLanDungThuoc.Text = dmVatTuEntity.LanDung.ToString();
                lkDonViDungThuoc.EditValue = dmVatTuEntity.MaDonViDung;
                txtSoLuongDungThuoc.Text = dmVatTuEntity.SoLuongDung.ToString();
                lkGhiChuCachDung.EditValue = dmVatTuEntity.MaGhiChuCachDung;
            }
            else
            {
                ClearData();
            }
        }

        private void lkLoaiVatTu_EditValueChanged(object sender, EventArgs e)
        {
            LoadPhanLoaiVt(lkLoaiVatTu.EditValue.ToString());
        }

        private void lkPhanLoaiVatTu_EditValueChanged(object sender, EventArgs e)
        {
            LoadDmVatTu(lkPhanLoaiVatTu.EditValue.ToString());
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void ClearData()
        {
            lkDmVatTu.EditValue = ConstantManagerClinic.sValueMember;
            lkDonVi.EditValue = ConstantManagerClinic.sValueMember;
            lkCachDungThuoc.EditValue = ConstantManagerClinic.sValueMember;
            txtSoLanDungThuoc.Text = "0";
            lkDonViDungThuoc.EditValue = ConstantManagerClinic.sValueMember;
            txtSoLuongDungThuoc.Text = "0";
            lkGhiChuCachDung.EditValue = ConstantManagerClinic.sValueMember;
            txtSoLuong.Text = "0";
            lkDmVatTu.Focus();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkLoaiVatTu, "Loại vật tư") == false) return;
            if (Validation.ValidLookUpEdit(lkPhanLoaiVatTu, "Phân loại vật tư") == false) return;
            if (Validation.ValidLookUpEdit(lkDmVatTu, "Vật tư") == false) return;
            if (Validation.ValidIsnumber(txtSoLanDungThuoc, "Số lần dùng") == false) { return; }
            if (Validation.ValidIsnumber(txtSoLuongDungThuoc, "Số lượng dùng") == false) { return; }
            if (Validation.ValidIsnumber(txtSoLuong, "Số lượng") == false) { return; }

            var donThuocChiTietEntity = new DonThuocChiTietEntity()
            {
                MaVatTu = lkDmVatTu.EditValue.ToString(),
                MaDonThuoc = maDongThuoc,
                SoLuong = Convert.ToDouble(txtSoLuong.Text),
                MaCachDung = lkCachDungThuoc.EditValue.ToString(),
                LanDung = Convert.ToDouble(txtSoLanDungThuoc.Text),
                SoLuongDung = Convert.ToDouble(txtSoLuongDungThuoc.Text),
                MaDonVi = lkDonVi.EditValue.ToString(),
                MaDonViDung = lkDonViDungThuoc.EditValue.ToString(),
                MaGhiChuCachDung = lkGhiChuCachDung.EditValue.ToString(),
                MaLoaiVatTu = lkLoaiVatTu.EditValue.ToString(),
                MaPhanLoaiVatTu = lkPhanLoaiVatTu.EditValue.ToString()
            };

            if (state == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (donThuocChiTietBusiness.GetDonThuocChiTiet(donThuocChiTietEntity.MaVatTu, maDongThuoc) != null)
                {
                    MessageBox.Show("Thuốc này đã tồn tại trong đơn!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                    return;
                }
                donThuocChiTietBusiness.InsertDonThuocChiTiet(donThuocChiTietEntity);
                ClearData();
            }
            else
            {
                donThuocChiTietBusiness.EditDonThuocChiTiet(donThuocChiTietEntity);
            }
            if (donThuocChiTietEvent != null)
                donThuocChiTietEvent(sender, e);
        }

        private void frmDonThuocChiTiet_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
    }
}